package com.ocom.exposeapi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.ExposeApiUrl;
import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.MD5;
import com.ocom.common.utils.MD5Util;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.pay.ABCRechargeVo;
import com.ocom.common.vo.pay.UpdateListRechargeVo;
import com.ocom.exposeapi.feign.PayConfigClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@Slf4j
@RestController
public class RechargeController {

    @Autowired
    private PayConfigClientService payConfigClientService;

    //.net农行充值调用查询人员状态
    @PostMapping(ExposeApiUrl.URL_ExposeApi_payGetOnePersonInfo)
    public Result getPageChannel(@RequestBody Map<String, Object> map){
        if (map.get("comId") ==null || map.get("pCode")==null || map.get("pName")==null){
            return ResultUtil.error(ResultEnum.Err_203);
        }
        try {
            if (requestJSON(map)){
                Integer comId = (Integer) map.get("comId");
                String pCode= String.valueOf(map.get("pCode"));
                String pName= (String) map.get("pName");
                return payConfigClientService.getPageChannel(Long.valueOf(comId),pCode,pName);
            }else {
                return ResultUtil.error(ResultEnum.Err_4041);
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(ResultEnum.Err_4041);
        }
    }

    @PostMapping(ExposeApiUrl.URL_ExposeApi_payRecharge)
    public Result payRecharge(@RequestBody Map<String, Object> map){

        try {
            if (requestJSON(map)){
                map.remove("Sign");
                ABCRechargeVo updateListRechargeVo= JSON.parseObject(JSON.toJSONString(map),ABCRechargeVo.class);
                return payConfigClientService.abcRecharge(updateListRechargeVo);
            }else {
                return ResultUtil.error(ResultEnum.Err_4041);
            }
        }catch (Exception e){
            return ResultUtil.error(ResultEnum.Err_4041);
        }
    }

    public static boolean requestJSON(Map<String, Object> map){

        String Sign = String.valueOf(map.get("Sign"));
        if (map==null){
            return false;
        }
        SortedMap<String, Object> parameters = new TreeMap<String, Object>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key=entry.getKey().toLowerCase();
            parameters.put(key,entry.getValue());
        }
        String strEncryption=createSign(parameters,"2D7E7C96-DAC5-4526-96C3-C60CDEC4B120");
        if (Sign.equals(strEncryption)){
            return true;
        }else {
            return false;
        }
    }

    public static String createSign(SortedMap<String,Object> parameters,String key){
        StringBuffer sb = new StringBuffer();
        Set es = parameters.entrySet();//所有参与传参的参数按照accsii排序（升序）
        Iterator it = es.iterator();
        while(it.hasNext()) {
            Map.Entry entry = (Map.Entry)it.next();
            String k = (String)entry.getKey();
            Object v = entry.getValue();
            if(null != v && !"".equals(v)
                    && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }

        sb=sb.append("key="+key);
        //MD5加密,结果转换为大写字符
        //String sign = MD5Util.MD5Encode(sbkey.toString(), characterEncoding).toUpperCase();
        String sign = MD5.MD5Encode(sb.toString(),"UTF-8");
        return sign;
    }


    @RequestMapping("/ocom/redirect")
    public void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.sendRedirect("http://gzh.ocom30.com/");
    }

}
